package cn.icare.cms.impl;

import cn.icare.cms.CommonCodeUtil;
import cn.icare.cms.ListUtil;
import cn.icare.cms.MoneyOutService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.MoneyOutMapper;
import cn.icare.cms.dao.po.MoneyOut;
import cn.icare.cms.model.enums.AuditStatusEnum;
import cn.icare.cms.model.enums.CommonStatusEnum;
import cn.icare.cms.model.ro.AddMoneyOutRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MoneyOutServiceImpl implements MoneyOutService {

    @Resource
    private MoneyOutMapper moneyOutMapper;

    private final Logger logger = LoggerFactory.getLogger(MoneyOutServiceImpl.class);



    @Override
    public Long createOne(AddMoneyOutRO ro) {
        try {
            MoneyOut moneyOut = new MoneyOut();
            moneyOut.setCode(CommonCodeUtil.getCodeByUUId());
            moneyOut.setOrderid(ro.getOrderid());
            moneyOut.setOrdercode(ro.getOrdercode());
            moneyOut.setCreatetime(new Date());
            moneyOut.setAmount(ro.getAmount());
            moneyOut.setMembername(ro.getMembername());
            moneyOut.setPaytype(ro.getPaytype());
            moneyOut.setPayaccount(ro.getPayaccount());
            moneyOut.setReason(ro.getReason());
            moneyOut.setStatus(AuditStatusEnum.WAIT_AUDIT.getCode());//待审核
            int result = moneyOutMapper.insertSelective(moneyOut);
            if (result > 0) {
                return moneyOut.getId();
            }
        }catch (Exception e)
        {
            this.logger.error("createOne 异常",e);
        }
        return 0L;
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {

        MoneyOut moneyOut=new MoneyOut();
        moneyOut.setId(id);
        moneyOut.setStatus(status);
        int result=this.moneyOutMapper.updateByPrimaryKeySelective(moneyOut);
        return  result>0;
    }

    @Override
    public boolean deleteOne(Long id) {

        return this.moneyOutMapper.deleteByPrimaryKey(id)>0;
    }

    @Override
    public boolean deleteBatch(List<Long> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(MoneyOut.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.moneyOutMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);

        }
        return result;
    }

    @Override
    public MoneyOut getOne(Long id) {

        return this.moneyOutMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getList(String code, String orderCode,String memberName,Integer status, Integer pageNum, Integer pageSize) {

        MsgDatasVO result = new MsgDatasVO();
        try {

            PageHelper.startPage(pageNum, pageSize);
            Example example = new Example(MoneyOut.class);
            Example.Criteria criteria = example.createCriteria();
            if(StringUtil.isNotEmpty(memberName))
            {
                criteria.andLike("membername","%"+memberName+"%");
            }
            if (StringUtil.isNotEmpty(code)) {
                //流水号
                criteria.andEqualTo("code", code);
            }
            if (StringUtil.isNotEmpty(orderCode)) {
                //订单号
                criteria.andEqualTo("ordercode", orderCode);
            }
            if(status>-1)
            {
                criteria.andEqualTo("status", status);
            }
            List<MoneyOut> dataList = this.moneyOutMapper.selectByExample(example);
            PageInfo pageInfo = new PageInfo(dataList);
            result.setTotal(pageInfo.getTotal());
            result.setDatas(dataList);

        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }
}
